#include "pc_comm.h"
#include "usbd_cdc.h"
#include "usbd_cdc_if.h"
#include "stm32f4xx_it.h"
#include "protocol.h"

static PC_Recv_s recv_data;
static PC_Send_s send_data;

uint8_t PCVisionDecode(PC_Recv_s* recv_data, uint8_t* buf)
{
    uint8_t res = 0;
    if(FRAME_HEADER == buf[SOF_ADDR])
    {
        res = verify_crc16_check_sum(buf, LEN_RX_PACKET);
        if(1 == res)
        {
            memcpy(recv_data, buf, sizeof(PC_Recv_s));
        }
    }
    return res;
}

void PCVisionSetFlag(Fire_State_e fire_state, 
                     Auto_Fire_Flag_e auto_flag, 
                     Enemy_Color_e enemy_color)
{
    send_data.tx_data.fire_state = fire_state;
    send_data.tx_data.IsAuotoAim = auto_flag;
    send_data.tx_data.enemy_color = enemy_color;
}

void PCVisionSetAttitude(float yaw, float pitch, float shoot_speed)
{
    send_data.tx_data.curr_yaw = yaw;
    send_data.tx_data.curr_pitch = pitch;
    send_data.tx_data.shoot_speed = shoot_speed;
}

void PCVisionInit()
{
    MX_USB_DEVICE_Init();
		USBD_Interface_fops_FS.Init();
}

void PCVisionSend()
{
    static uint8_t pc_send_buf[LEN_TX_PACKET + 1];
    memcpy(pc_send_buf, &send_data, sizeof(PC_Send_s));
    pc_send_buf[LEN_TX_PACKET] = '\n';
    CDC_Transmit_FS(pc_send_buf, LEN_TX_PACKET + 1);
}
